package com.nd.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.AdMapper;
import com.nd.pojo.bean.Ad;
import com.nd.pojo.bean.AdExample;
import com.nd.pojo.bo.AdAddBo;
import com.nd.pojo.bo.AdDeleteBo;
import com.nd.pojo.vo.AdAddVo;
import com.nd.pojo.vo.AdQueryDataVo;
import com.nd.pojo.vo.AdQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class AdServiceImpl implements AdService{

    @Autowired
    AdMapper adMapper;

    @Override
    public AdQueryDataVo queryAll(Integer page, Integer limit, String sort, String order, String name, String content) {

        //执行查询之前  使用分页插件  会在执行过程中拼接上sql语句
        PageHelper.startPage(page, limit);
        //username = “test1”  nickname like 测试用户
        //每个service的方法都来创建example对象
        AdExample adExample = new AdExample();

        //排序信息
        adExample.setOrderByClause(sort + " " + order);
        AdExample.Criteria criteria = adExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (content != null && !"".equals(content)) {
            criteria.andContentLike("%" + content + "%");
        }



        List<Ad> ads = adMapper.selectByExample(adExample);
        List<AdQueryVo> adQueryVos = new ArrayList<>();

        for (Ad ad : ads) {
            AdQueryVo adQueryVo = new AdQueryVo(ad.getId(), ad.getName(), ad.getLink(), ad.getUrl(),
                    ad.getPosition(),ad.getContent(), ad.getEnabled(), ad.getAddTime(), ad.getUpdateTime(),ad.getDeleted());
            adQueryVos.add(adQueryVo);

        }


        //可以获得分页信息
        PageInfo<Ad> userPageInfo = new PageInfo<>(ads);
        long total = userPageInfo.getTotal();

        AdQueryDataVo adQueryDataVo = new AdQueryDataVo();
        adQueryDataVo.setTotal((int)total);
        adQueryDataVo.setItems(adQueryVos);


        return adQueryDataVo;
    }

    @Override
    public Integer delete(AdDeleteBo adDeleteBo) {

        AdExample adExample = new AdExample();

        AdExample.Criteria criteria = adExample.createCriteria();

        criteria.andIdEqualTo(adDeleteBo.getId());

        Ad ad = new Ad();
        ad.setDeleted(true);

        Integer code = adMapper.updateByExampleSelective(ad, adExample);

        return code;

    }

    @Override
    public Ad create(AdAddBo adAddBo) {

        Ad ad = new Ad();
        ad.setName(adAddBo.getName());
        ad.setContent(adAddBo.getContent());
        ad.setUrl(adAddBo.getUrl());
        ad.setLink(adAddBo.getLink());
        ad.setPosition(adAddBo.getPosition());
        ad.setEnabled(adAddBo.getEnabled());
        //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //df.format(new Date()); new Date()为获取当前系统时间

        Date date = new Date();

        ad.setAddTime(date);
        ad.setUpdateTime(date);

        Integer code = adMapper.insertSelective(ad);


        return ad;
    }

    @Override
    public Ad update(Ad ad) {

        AdExample adExample = new AdExample();

        AdExample.Criteria criteria = adExample.createCriteria();

        criteria.andIdEqualTo(ad.getId());

        int i = adMapper.updateByExampleSelective(ad, adExample);

        return ad;
    }
}
